const path = require('path')
const autoprefixer = require('autoprefixer')
const pxtorem = require('postcss-pxtorem')
const vConsolePlugin = require('vconsole-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const resolve = dir => path.join(__dirname, dir);
const IS_DEVELOPMENT = ["development"].includes(process.env.NODE_ENV);
const IS_PRODUCTION = ["production"].includes(process.env.NODE_ENV);

module.exports = {
    //publicPath: !IS_DEVELOPMENT ? process.env.VUE_APP_PUBLIC_PATH : "./", // 默认'/'，部署应用包时的基本 URL
    publicPath: "./", // 默认'/'，部署应用包时的基本 URL
    outputDir: process.env.VUE_APP_NAME,
    lintOnSave: false,
    runtimeCompiler: false, 
    productionSourceMap: IS_DEVELOPMENT,
    parallel: require("os").cpus().length > 1,
    pwa: {},
    devServer: {
        host: '0.0.0.0',
        port: 9999,
        proxy: {
            '/shhhxcch': {
              target:'http://rap2api.taobao.org/app/mock/277981',  //shu
              ws: false,
              secure: true, // 如果是https接口，需要配置这个参数
              changeOrigin: true, // 是否跨域
              pathRewrite: {
                '^/shhhxcch': ''
              }
            }
        }
    },
    chainWebpack: config => {
      // 添加别名
      config.resolve.alias
        .set("@", resolve("src"))
        .set("@assets", resolve("src/assets"))
        .set("@components", resolve("src/components"))
        .set("@views", resolve("src/views"))
        .set("@router", resolve("src/router"))
        .set("@store", resolve("src/store"))
    },
    css: {
      loaderOptions: {
        postcss: {
            plugins: [
              autoprefixer(),
              pxtorem({
                rootValue: 37.5,
                propList: ['*'],
                /*该项仅在使用 Circle 组件时需要*/ 
                /*原因参见 https://github.com/youzan/vant/issues/1948*/
                selectorBlackList: ['van-circle__layer']
              })
            ]
        },
        less: {
          modifyVars: {
            /*或者可以通过 less 文件覆盖（文件路径为绝对路径）*/ 
            hack: `true; @import "${path.join(
              __dirname,
              "./src/common/themevar.less"//这个import 的路径必须是绝对路径
            )}";`
          },
        },
      },
    },
    configureWebpack: config => {
       //生产环境去掉vconsole调试器
      const IS_DEVELOPMENT_OR_TEST = ["development","test"].includes(process.env.NODE_ENV);
      let envType = IS_DEVELOPMENT_OR_TEST;
      let pluginsDev = [
         new vConsolePlugin({
             filter: [],
             enable: envType
         })
      ]
      if(!IS_DEVELOPMENT_OR_TEST){
        pluginsDev.push(
          new UglifyJsPlugin({
            uglifyOptions: {
              output: {
                comments: false, // 去掉注释
              },
              warnings: false,
              compress: {
                drop_console: true,
                drop_debugger: false,
                pure_funcs: ['console.log']//移除console
              }
            }
          })
        )
      }
      config.plugins = [...config.plugins, ...pluginsDev]
    }
}
